package cn.xgj.sfzy.service.impl;

import cn.xgj.sfzy.entity.*;
import cn.xgj.sfzy.mapper.*;
import cn.xgj.sfzy.service.CourseReplyService;
import cn.xgj.sfzy.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CourseReplyServiceImpl
 * @Description TODO
 * @Author furuyue
 * @Date 2024年10月28日 17:26
 * @Version 1.0
 */
@Service
public class CourseReplyServiceImpl implements CourseReplyService {

    @Autowired
    private CourseReplyMapper courseReplyMapper;
    @Autowired
    private CourseTalkMapper courseTalkMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<CourseReply> findByUSerId(Long userId) {

        List<CourseReply> list = courseReplyMapper.findByUserId(userId);
        for(CourseReply a :list){
            Long courseTalkId = a.getCourseTalkId();
            CourseTalk courseTalk = courseTalkMapper.findById(courseTalkId);

            Long userId1 = courseTalk.getUserId();
            User user = userMapper.findById(userId1.intValue());

            courseTalk.setUser(user);

            a.setCourseTalk(courseTalk);
        }
        return list;
    }

    @Override
    public List<CourseReply> findByCourseTalkId(Long courseTalkId) {
        List<CourseReply> courseReplyList = courseReplyMapper.findByCourseTalkId(courseTalkId);
        for (CourseReply courseReply : courseReplyList) {
            if (courseReply.getUserId()==null){
                return courseReplyList;
            }
            User user = userMapper.findById(Math.toIntExact(courseReply.getUserId()));
            courseReply.setUser(user);
        }
        return courseReplyList;
    }
}
